package com.wanlian.common;

import com.wanlian.util.DateUtil;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.util.Date;

public class IsHoliday {


    public static Boolean isHoliday() throws ParseException {
        String dateNow = DateUtil.formatDate(new Date());
        String date = dateNow.substring(0, 10).replaceAll("-", "");
        int hour = Integer.parseInt(dateNow.replaceAll(" ", "").substring(10, 12).replaceAll(":", ""));
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        String httpUrl = "http://api.goseek.cn/Tools/holiday?date=" + date;
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
            JSONObject jsonstr = new JSONObject(result);
            int code = Integer.parseInt(jsonstr.get("data") + "");
            ////0 上班 1周末 2节假日
            if (code == 0) {
                if (hour < 9 || hour > 16) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

}
